package com.example.core.erWeiMa;

/**
 * @ClassName QrcodeController
 * @Author zhangzhaoyang
 * @Date 2019/9/26 17:04
 **/

import com.google.zxing.Result;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 二维码调用前端控制器
 */
@RequestMapping("/Qrcode")
@RestController
public class QrcodeController {
    private static Logger logger = LoggerFactory.getLogger(QrcodeController.class);
    /**
     * 生成二维码
     */
    @GetMapping("/test")

    public void productcode(HttpServletResponse response, @Param("name") String name) throws IOException {
        QRCodeUtil.zxingCodeCreate("https://www.taobao.com/", "D:/1111111/",500,"D:/1111111/5.jpg");
        logger.info("==========="+name);
        String content = name;
        Integer size = 500;
        String logoPath = "D:/1111111/5.jpg";
        String path = "D:/1111111/";




        //图片类型
        String imageType = "jpg";
        //获取二维码流的形式，写入到目录文件中
        BufferedImage image = QRCodeUtil.getBufferedImage(content, size, logoPath);

        //获得随机数
//        Random random = new Random();
//        //生成二维码存放文件
//        File file = new File(path+random.nextInt(1000)+".jpg");
//        if (!file.exists()) {
//            file.mkdirs();
//        }

        if(image!=null){
            try {
                ImageIO.write(image, "jpg", response.getOutputStream());
            } catch (IOException e) {
                e.printStackTrace();
                System.out.println("打印异常:com.controller.Business_Ctrl.getImg(String, HttpServletResponse)");
            }
        }


    }


    /**
     * 解析二维码
     */
    @GetMapping("/test1")
    public void analysiscode() {
        Result result = QRCodeUtil.zxingCodeAnalyze("D:/1111111/505.jpg");
        System.err.println("二维码解析内容："+result.toString());
    }

}
